package com.suikajy.damaiproject.ui.main_class;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.suikajy.damaiproject.R;
import com.suikajy.damaiproject.util.PullDownRefreshLayout;
import com.suikajy.damaiproject.util.RefreshBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClassFragment extends Fragment {

    @Bind(R.id.location_selector)
    TextView mLocationSelector;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.filter)
    TextView mFilter;
    @Bind(R.id.title_bar_layout)
    RelativeLayout mTitleBarLayout;
    @Bind(R.id.tabLayout)
    TabLayout mTabLayout;
    @Bind(R.id.vp_class)
    ViewPager mVpClass;
    @Bind(R.id.tv_refresh)
    TextView mTvRefresh;
    @Bind(R.id.refresh_bar)
    RefreshBar mRefreshBar;
    @Bind(R.id.refresh_layout)
    PullDownRefreshLayout mRefreshLayout;
    private MyViewPagerAdapter mAdapter;
    private List<String> mTabTitles = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View mView = inflater.inflate(R.layout.fragment_class, container, false);
        ButterKnife.bind(this, mView);
        initTabTitles();
        initView();
        return mView;
    }

    private void initTabTitles() {
        mTabTitles.add("全部分类");
        mTabTitles.add("演唱会");
        mTabTitles.add("音乐会");
        mTabTitles.add("话剧歌剧");
        mTabTitles.add("舞蹈芭蕾");
        mTabTitles.add("曲苑杂坛");
        mTabTitles.add("体育比赛");
        mTabTitles.add("度假休闲");
        mTabTitles.add("周边商品");
        mTabTitles.add("儿童亲子");
        mTabTitles.add("动漫");
    }

    private void initView() {
        for (int i = 0; i < mTabTitles.size(); i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(mTabTitles.get(i)));
        }
        mAdapter = new MyViewPagerAdapter(getChildFragmentManager());
        mVpClass.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mVpClass);
        mRefreshLayout.setPullDownTextView(mTvRefresh);
        mRefreshLayout.setRefreshBar(mRefreshBar);
    }

    public PullDownRefreshLayout getRefreshLayout() {
        return mRefreshLayout;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onDestroy() {
        mAdapter = null;
        super.onDestroy();
    }

    private class MyViewPagerAdapter extends FragmentPagerAdapter {

        private SparseArray<ClassContentFragment> mFragments;

        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);
            initFragment();
        }

        private void initFragment() {
            mFragments = new SparseArray<>();
            for (int i = 0; i < 11; i++) {
                ClassContentFragment fragment = new ClassContentFragment();
                Bundle args = new Bundle();
                args.putInt("mc", i);
                fragment.setArguments(args);
                mFragments.append(i, fragment);
            }
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTabTitles.get(position);
        }
    }
}
